package com.exp.quick_backend.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tk.mybatis.mapper.entity.Example;

import com.exp.base.action.BaseAction;
import com.exp.base.action.EasyuiPage;
import com.exp.base.action.EasyuiPageInfo;
import com.exp.base.action.Message;
import com.exp.quick_backend.entity.EntityExample;
import com.exp.quick_backend.service.EntityExampleService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;

@Controller
@RequestMapping("/quick_backend/entityExample")
public class EntityExampleAction extends BaseAction {
	@Resource
	EntityExampleService entityExampleService;
	
	
	@RequestMapping("add")
	public String add(Model model){
		EntityExample entityExample = new EntityExample();
		model.addAttribute("entityExample", entityExample);
		return "quick_backend/entity_example/entity_example_input";
	}

	@RequestMapping("edit")
	public String edit(Model model,@ModelAttribute("id") String id){
		EntityExample entityExample = entityExampleService.get(id);
		model.addAttribute("entityExample", entityExample);
		return "quick_backend/entity_example/entity_example_input";
	}

	@RequestMapping("list")
	public String list(){
		return "quick_backend/entity_example/entity_example_list";
	}
	
	@ResponseBody
	@RequestMapping("save")
	public Message save(HttpServletRequest request,EntityExample entityExample){
		List<String> errors = new ArrayList<String>();
		isValid(entityExample, errors);
		if(errors.isEmpty()){
			entityExampleService.save(entityExample);
			return new Message(true);
		}
		return new Message(false,StringUtils.join(errors,","));
	}

	@ResponseBody
	@RequestMapping("update")
	public Message update(HttpServletRequest request,EntityExample entityExample){
		List<String> errors = new ArrayList<String>();
		isValid(entityExample, errors);
		if(errors.isEmpty()){
			EntityExample entityExampleEntity = entityExampleService.get(entityExample.getId());
			filterParameterMapIgnore(request, entityExample, entityExampleEntity);
			entityExampleService.update(entityExampleEntity);
			return new Message(true);
		}
		return new Message(false,StringUtils.join(errors,","));
	}
	
	@ResponseBody
	@RequestMapping(value="delete")
	public Message delete( String[] ids){
		entityExampleService.delete(ids);
		return new Message(true);
	}
	
	@ResponseBody
	@RequestMapping("query")
	public EasyuiPageInfo query(Model model,EasyuiPage easyuiPage){
		Page<EntityExample> page = new Page<EntityExample>(easyuiPage.getPage(), easyuiPage.getRows());
		Example example = new Example(EntityExample.class);
		example.createCriteria().andEqualTo("deleted", false);
		example.setOrderByClause("create_date desc");
		PageInfo<EntityExample>  pageInfo = entityExampleService.query(example, page);
		EasyuiPageInfo easyuiPageInfo = new EasyuiPageInfo(pageInfo);
		return easyuiPageInfo;
	}



}
